package com.limeng.es.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SearchPositionConfig {

    @Value(value="${elasticsearch.hostlist}")
    private String hostlist;
    @Bean
    public RestHighLevelClient elasticsearchClient(){
        String[] ss = hostlist.split(",");
        HttpHost[] httpHost = new HttpHost[ss.length];
        for (int i = 0; i < ss.length; i++) {
            String s = ss[i];
            String[] hostIp = s.split(":");
            httpHost[i] = new HttpHost(hostIp[0],Integer.parseInt(hostIp[1]),"http");
        }
        return new RestHighLevelClient(RestClient.builder(httpHost));
    }
}
